
package com.voicecheck.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aris.voicechecking.LessonDetailActivity;
import com.aris.voicechecking.R;

public class DayReportAdapter extends BaseAdapter {

    protected int week;

    protected int day;

    protected String answeredList;

    protected String resultList;

    public DayReportAdapter(int weeknumber, int daynumber, String answerList, String rtList) {

        week = weeknumber;
        day = daynumber;
        answeredList = answerList;
        resultList = rtList;

    }

    @Override
    public int getCount() {
        return answeredList.length();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        Context ct = parent.getContext();

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_day_report, parent, false);

            holder = new ViewHolder();
            holder.title = (TextView)convertView.findViewById(R.id.day_report_title);
            holder.done_icon = (ImageView)convertView.findViewById(R.id.day_report_done_ic);
            holder.result_icon = (ImageView)convertView.findViewById(R.id.day_report_status_ic);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.title.setText(ct.getResources().getString(R.string.week) + week + "-"
                + ct.getResources().getString(R.string.day) + day + "-" + " ("
                + (position >= 3 ? 2 : 1) + "-" + (position % 3 + 1) + ")");

        if (resultList.charAt(position) == '1')
            holder.done_icon.setImageResource(R.drawable.btn_ok);
        else
            holder.done_icon.setImageResource(R.drawable.btn_try);

        holder.done_icon.setTag(position);
        holder.done_icon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int pos = Integer.parseInt(v.getTag().toString());
                if (resultList.charAt(pos) == '1') {

                } else {
                    go2RetryActivity(pos, v.getContext());
                }

            }
        });

        if (answeredList.charAt(position) == '-') {

            holder.result_icon.setImageResource(R.drawable.answer_none);
        } else {
            if (answeredList.charAt(position) == '0') {
                holder.result_icon.setImageResource(R.drawable.answer_pass);
            } else {
                holder.result_icon.setImageResource(R.drawable.answer_fail);
            }
        }

        return convertView;
    }

    protected void go2RetryActivity(int pos, Context ct) {

        Intent intent = new Intent(ct, LessonDetailActivity.class);

        intent.putExtra("daynumber", day);
        intent.putExtra("weeknumber", week);
        intent.putExtra("se", pos + 1);

        ct.startActivity(intent);

    }

    static class ViewHolder {
        TextView title;

        ImageView done_icon;

        ImageView result_icon;
    }

}
